package com.edward.service;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.FundPay;
import com.edward.model.Receivable;
import com.edward.model.ShouldPay;


public class FundPayManager extends HibernateDaoSupport {

	public void addFundPay(FundPay fundPay) {
		
		getHibernateTemplate().save(fundPay);
	}
	
	public void updateFundPay(FundPay fundPay) {
		
		getHibernateTemplate().update(fundPay);
	}
	
	public void deleteFundPay(int id){
		
		getHibernateTemplate().delete(getFundPay(id));
	}
	
	public FundPay getFundPay(int id){
		FundPay d =(FundPay)getHibernateTemplate().get(FundPay.class, id);
		return d;
	}
	

	@SuppressWarnings("unchecked")
	public List<FundPay> listFundPay() {
		return getHibernateTemplate().find("from FundPay");
	}

	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit,String type) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String d = sdf.format(new Date());
		List <FundPay>list = getHibernateTemplate().find("from FundPay f where f.type = '" + type +"' and f.date = '" + d +"'" );
		FundPay sp = new FundPay();
		for(FundPay s : list){
			sp.setDbank(sp.getDbank() + s.getDbank());
			sp.setDcash(sp.getDcash() + s.getDcash());
			sp.setRbank(sp.getRbank() + s.getRbank());
			sp.setRcash(sp.getRcash() + s.getRcash());
		}
		list.add(sp);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <FundPay>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit,FundPay fundPay) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		List <FundPay>list;
		String sql = "from FundPay f where f.type = '" + fundPay.getType() + "'";
		if(!fundPay.getCompany().equals("")){
			sql += "and f.company = '" + fundPay.getCompany() + "'";
		}
		if(!fundPay.getHandler().equals("")){
			
			sql += "and f.handler = '" + fundPay.getHandler() + "'";
		}
		if(fundPay.getDate() != null){
			String d = sdf.format(fundPay.getDate());
			
			sql += "and  f.date = '" + d + "'";
		}
		
		list = getHibernateTemplate().find(sql);
		FundPay sp = new FundPay();
		for(FundPay s : list){
			sp.setDbank(sp.getDbank() + s.getDbank());
			sp.setDcash(sp.getDcash() + s.getDcash());
			sp.setRbank(sp.getRbank() + s.getRbank());
			sp.setRcash(sp.getRcash() + s.getRcash());
		}
		list.add(sp);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <FundPay>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	@SuppressWarnings("unchecked")
	public void addChange(FundPay fundPay,String choose){
		if(choose.equals("应付")){
			List<ShouldPay> list = getHibernateTemplate().find("from ShouldPay s where s.name = '" + fundPay.getCompany() +"'" );
			if(list.size() != 0){
				ShouldPay sp = list.get(0);
				sp.setContent(fundPay.getContent());
				sp.setDate(fundPay.getDate());
				sp.setRbalance(sp.getRbalance() - fundPay.getRbank() -fundPay.getRcash());
				sp.setDbalance(sp.getDbalance() - fundPay.getDbank() -fundPay.getDcash());
				sp.setType(fundPay.getType());
				getHibernateTemplate().update(sp);
				if(sp.getRbalance() < 0 || sp.getDbalance() < 0){
					Receivable r = new Receivable();
					r.setContent(sp.getContent());
					r.setDate(sp.getDate());
					r.setRbalance(sp.getRbalance());
					r.setDbalance(sp.getDbalance());
					r.setName(sp.getName());
					r.setType(sp.getType());
					getHibernateTemplate().save(r);
				}
				
			}else{
				Receivable r = new Receivable();
				r.setContent(fundPay.getContent());
				r.setName(fundPay.getCompany());
				r.setDbalance(fundPay.getDbank() + fundPay.getDcash());
				r.setRbalance(fundPay.getRbank() + fundPay.getRcash());
				r.setDate(fundPay.getDate());
				r.setType(fundPay.getType());
				getHibernateTemplate().save(r);
			}
		}else if (choose.equals("应收")){
			List<Receivable> list = getHibernateTemplate().find("from Receivable r where r.name = '" + fundPay.getCompany() +"'" );
			if(list.size() != 0){
				Receivable r = list.get(0);
				r.setContent(fundPay.getContent());
				r.setDate(fundPay.getDate());
				r.setRbalance(r.getRbalance() - fundPay.getRbank() -fundPay.getRcash());
				r.setDbalance(r.getDbalance() - fundPay.getDbank() -fundPay.getDcash());
				r.setName(fundPay.getCompany());
				r.setType(fundPay.getType());
				getHibernateTemplate().update(r);
				if(r.getRbalance() < 0 || r.getDbalance() < 0){
					ShouldPay sp = new ShouldPay();
					sp.setContent(r.getContent());
					sp.setName(r.getName());
					sp.setDate(r.getDate());
					sp.setRbalance(r.getRbalance() );
					sp.setDbalance(r.getDbalance() );
					sp.setType(r.getType());
					getHibernateTemplate().save(sp);
				}
			}else{
				ShouldPay sp = new ShouldPay();
				sp.setContent(fundPay.getContent());
				sp.setName(fundPay.getCompany());
				sp.setDbalance(fundPay.getDbank() + fundPay.getDcash());
				sp.setRbalance(fundPay.getRbank() + fundPay.getRcash());
				sp.setDate(fundPay.getDate());
				sp.setType(fundPay.getType());
				getHibernateTemplate().save(sp);
			
			}
			
			
			
			
		}
		
		
	}
	
	


}



